<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class AccountForm extends CFormModel {
    
    public $no_account;
    public $account_type_id;
    public $currency_id;
    public $date_add;
    public $max_debet;
    public $worker_id;
    public $user_id;

    /**
     * Declares the validation rules.
     * The rules state that username and password are required,
     * and password needs to be authenticated.
     */
    public function rules() {
        return array(
            array('no_account, account_type_id, currency_id, date_add, max_debet, user_id', 'required', 'message' => 'Pole wymagane'),
            array('date_add', 'date', 'format' => 'yyyy-MM-dd', 'message' => 'Format daty to yyyy-MM-DD'),
        );
    }
            
    /**
     * Declares attribute labels.
     */
    public function attributeLabels() {
        return array(
            'no_account' => 'Numer rachunku',
            'account_type_id' => 'Rodzaj konta',
            'currency_id' => 'Waluta',
            'date_add' => 'Data założenia',
            'max_debet' => 'Maks. zadłużenie',
            'worker_id' => 'Pracownik',
            'user_id' => 'Użytkownik',
        );
    }
   
    public function addAccount(){
        $this->worker_id = Yii::app()->user->id;
        $this->no_account = str_replace(" ", "", $this->no_account);
        return Yii::app()->db->createCommand("SELECT createkontofun('".$this->no_account."', '".$this->account_type_id."', '".$this->currency_id."', '".$this->date_add."', '".$this->max_debet."', '".$this->worker_id."', '".$this->user_id."' )")->execute();
//        return Yii::app()->db->createCommand("SELECT createworkerfun('".$this->firstname."', '".$this->lastname."', '".$this->role_id."', '".$this->login."', '".md5($this->pass_hash)."')")->execute();
    }
    
//    public function editWorker($id){
//        return Yii::app()->db->createCommand("SELECT editworkerfun('".$id."', '".$this->firstname."', '".$this->lastname."', '".$this->role_id."', '".$this->login."', '".md5($this->pass_hash)."')")->execute();
//    }
    
    public static function accountTypeList(){
        return Yii::app()->db->createCommand()->select('*')->from('rodzaj_konta rk')->queryAll();
    }
    
    public static function currencyList(){
        return Yii::app()->db->createCommand()->select('*')->from('waluty w')->queryAll();
    }
    
    public static function usersList(){
        $users = Yii::app()->db->createCommand()->select('*')->from('uzytkownicy u')->queryAll();
        foreach($users as $i => $v){
            $users[$i]['id'] = $v['id_uzytkownika'];
            $users[$i]['name'] = $v['nazwisko'].' '.$v['imie'].' '.$v['pesel'];
        }
        return $users;
    }
}
